import axios from 'axios'
import { message } from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';
import router from './router';


const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    headers: { 'Content-Type': 'application/json;charset=utf-8' },
    timeout: 120000 // 指定请求超时的毫秒数(0 表示无超时时间)
  })

//写入请求头
service.interceptors.request.use(config =>{
    config.headers["authorization"] = window.sessionStorage.getItem('token')
    return config
})


//拦截异常code
service.interceptors.response.use(response => {
    let res = response.data
    if(res.code === 200){
        return response
    }else{
        console.log(res)
        message.error(res?res.msg:'获取数据异常')
        return Promise.reject(response.data.msg)
    }
},error=>{
    if(error.message.data){
        error.message = error.response.msg
    }
    if(error.response.status === 400)
        router.push('/Login')
    return Promise.reject(error)
})

export default service